package com.binhthuan.catechist.client.component;

import gwtupload.client.IUploader;
import gwtupload.client.SingleUploader;
import gwtupload.client.IFileInput.FileInputType;

import com.google.gwt.event.dom.client.ErrorEvent;
import com.google.gwt.event.dom.client.ErrorHandler;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.XMLParser;
import com.smartgwt.client.types.Cursor;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.VLayout;

public class ImageUploader extends SingleUploader {

    public static final String[] IMAGE_EXTENSIONS = new String[] { "jpg", "gif", "png" };
    private static final String HINT_STYLE = "ImageUploader-hint";
    private Image image;
    private String blankImageUrl;
    private Label hint;

    public ImageUploader() {
        this(null);
    }

    public ImageUploader(String blankImageUrl) {
        this(blankImageUrl, null);
    }

    public ImageUploader(String blankImageUrl, String servletPath) {
        this(blankImageUrl, servletPath, "100px", "100px");
    }

    public ImageUploader(String blankImageUrl, String width, String height) {
        this(blankImageUrl, null, width, height);
    }

    public ImageUploader(String blankImageUrl, String servletPath, String width, String height) {
        super();
        this.blankImageUrl = blankImageUrl;
        setAutoSubmit(true);
        setValidExtensions(IMAGE_EXTENSIONS);
        setServletPath(servletPath);
        addOnFinishUploadHandler(new OnFinishUploaderHandler() {

            @Override
            public void onFinish(IUploader uploader) {
                switch (uploader.getStatus()) {
                case SUCCESS:
                    UploadedInfo info = uploader.getServerInfo();
                    if (info != null) {
                        Document doc = XMLParser.parse(info.message);
                        String url = Utils.getXmlNodeValue(doc, "file-1-url");
                        image.setUrl(url);
                    }
                    break;
                default:
                    uploader.reset();
                    break;
                }
            }
        });

        image = new Image();
        image.addErrorHandler(new ErrorHandler() {
            
            @Override
            public void onError(ErrorEvent event) {
                image.setUrl(ImageUploader.this.blankImageUrl);
            }
        });
        setImageHeight(height);
        setImageWidth(width);
        image.setUrl(this.blankImageUrl);
        setFileInput(FileInputType.CUSTOM.with(getImage()).getInstance());
    }

    /* (non-Javadoc)
     * @see gwtupload.client.Uploader#reset()
     */
    @Override
    public void reset() {
        super.reset();
        image.setUrl(this.blankImageUrl);
    }

    /**
     * @return the image
     */
    public Image getImage() {
        if (image == null) {
            image = new Image();
        }
        return image;
    }

    /**
     * @param image
     *            the image to set
     */
    public void setImage(String image) {
        this.image.setUrl(image);
    }

    /**
     * @return the imageHeight
     */
    public int getImageHeight() {
        return getImage().getHeight();
    }

    /**
     * @param imageHeight
     *            the imageHeight to set
     */
    public void setImageHeight(String imageHeight) {
        getImage().setHeight(imageHeight);
    }

    /**
     * @return the imageWidth
     */
    public int getImageWidth() {
        return getImage().getWidth();
    }

    /**
     * @param imageWidth
     *            the imageWidth to set
     */
    public void setImageWidth(String imageWidth) {
        getImage().setWidth(imageWidth);
    }

    /**
     * @return the blankImageUrl
     */
    public String getBlankImageUrl() {
        return blankImageUrl;
    }

    /**
     * @param blankImageUrl
     *            the blankImageUrl to set
     */
    public void setBlankImageUrl(String blankImageUrl) {
        this.blankImageUrl = blankImageUrl;
        image.setUrl(this.blankImageUrl);
    }

    private Label getHintLabel() {
        if (hint == null) {
            hint = new Label(getI18NConstants().uploaderBrowse());
            hint.setStyleName(HINT_STYLE);
        }
        return hint;
    }

    /**
     * @return the hint
     */
    public String getHint() {
        return getHintLabel().getTitle();
    }

    /**
     * @param hint
     *            the hint to set
     */
    public void setHint(String hint) {
        getHintLabel().setText(hint);
    }

    public void setHintStyle(String styleName) {
        getHintLabel().setStyleName(styleName);
    }

    public Canvas createCanvas() {
        return createCanvas(false);
    }

    public Canvas createCanvas(boolean showHint) {
        VLayout layout = new VLayout();
        layout.setCursor(Cursor.POINTER);

        VerticalPanel panel = new VerticalPanel();
        panel.add(this);
        if (showHint) {
            panel.add(getHintLabel());
        }

        layout.addMember(panel);
        return layout;
    }
}
